﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace LabMS.Web.Role
{
    public partial class ShowUser : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("JSGL");
            strPageUserType.Add("mana");
            if (!IsPostBack)
            {
                if (Request.QueryString["RoleId"] == null)
                {
                    Common.JShelper.JSAlert(this.Page, "", "你无权查看本页面！");
                }
                else
                {
                    int RoleId = int.Parse(Request.QueryString["RoleId"].ToString());
                    InitBind(RoleId);
                }
            }
        }

        /// <summary>
        ///页面绑定 
        /// </summary>
        protected void InitBind(int RoleId)
        {
            DataSet ds = new DataSet();
            ExtendBLL.UserRole EBUR = new ExtendBLL.UserRole();
            try
            {
                ds = EBUR.GetUserInfoByRoleID(RoleId);
            }
            catch(Exception ex)
            {
                if (ex.Message == "2")
                {
                    //Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"","<script type='text/javascript'>alert('该角色还没有分配给用户！');</script>");
                    Common.JShelper.JSAlert(this.Page, "", "该角色还没有分配给用户！");
                    return;                    
                }
            }
            ds=HandleDataSet(ds);
            gvRole.DataSource = ds.Tables[0].DefaultView;
            gvRole.DataBind();
        }

        /// <summary>
        /// 把RealName加到dataset中
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        protected DataSet HandleDataSet(DataSet ds)
        {
            ds.Tables[0].Columns.Add("RealName");
            string PCode = "";
            ExtendBLL.UserTable EBUT = new ExtendBLL.UserTable();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                PCode = ds.Tables[0].Rows[i]["PCode"].ToString();
                ds.Tables[0].Rows[i]["RealName"] = EBUT.GetRealNameByPCode(PCode);
            }
            return ds;

        }

        protected void gvRole_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvRole.PageIndex = e.NewPageIndex;
            int RoleId = int.Parse(Request.QueryString["RoleId"].ToString());
            InitBind(RoleId);
        }

        protected void gvRole_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void gvRole_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'");
                //e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#6699FF';this.style.color='#8C4510'");

                int item = e.Row.Cells.Count;
                for (int i = 0; i < item; i++)
                {
                    e.Row.Cells[i].Attributes.Add("OnDblClick", "window.open('../User/EditUser.aspx?UserId=" + gvRole.DataKeys[e.Row.DataItemIndex].Value.ToString() + "','newwindow','top=200,left=250,height=400,width=400')");
                }

                e.Row.Attributes["style"] = "Cursor:pointer";
            }
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnserach_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(tbUserName.Text.Trim()))
            {
                //Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"","<script type='text/javascript'>alert('请输入要搜索的用户名');</script>");
                Common.JShelper.JSAlert(this.Page, "", "请输入要搜索的用户名!");
                tbUserName.Focus();
            }
            else
            {
                string str = "UserName like '%" + tbUserName.Text.Trim() + "%'";
                LabMS.BLL.UserTable BUT = new LabMS.BLL.UserTable();
                DataSet ds = BUT.GetList(str);
                ds = HandleDataSet(ds);
                gvRole.DataSource = ds.Tables[0].DefaultView;
                gvRole.DataBind();
            }
        }
    }
}
